﻿using Domain.Entitys.DB;
using Domain.IRepositorys.DB;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repositorys.DB
{
    public class Db_ProductRepository : IDb_ProductRepository
    {
        private readonly MyDbContext myDbContext;

        public Db_ProductRepository(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }

        /// <summary>
        /// 验证序号是不是唯一
        /// </summary>
        /// <param name="sort"></param>
        /// <param name="proId"></param>
        /// <returns></returns>
        public bool CheckSortUnique(int sort, int proId)
        {
            var unique = myDbContext.Db_Product
                .Where(p => p.Pro_sort == sort);
            if (proId > 0)
            {
                unique = unique.Where(p => p.Pro_id != proId);
            }
            return unique.Any();
        }

        /// <summary>
        /// 根据id查询对应的产品信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Db_Product> GetDb_ProductByCode(string code)
        {
            return await myDbContext.Db_Product.FirstOrDefaultAsync(p => p.Pro_code == code);
        }

        /// <summary>
        /// 分页条件查询产品信息
        /// </summary>
        /// <param name="proName"></param>
        /// <param name="typeCode"></param>
        /// <returns></returns>
        public List<Db_Product> Get_Products(string proName, string typeCode, int currentPage, int pageSize)
        {
            IQueryable<Db_Product> query = myDbContext.Db_Product.Where(p => p.Del_flag == '0');
            if (!string.IsNullOrEmpty(proName))
            {
                query = query.Where(p => p.Pro_name.Contains(proName) || p.Pro_code.Contains(proName));
            }
            if (!string.IsNullOrEmpty(typeCode))
            {
                query = query.Where(p => p.Pro_type_code == typeCode);
            }
            return query.OrderBy(d => d.Pro_sort).Skip((currentPage - 1) * pageSize).Take(pageSize).OrderBy(p => p.Pro_sort).ToList();
        }
        /// <summary>
        /// 查询产品信息----其他入库单
        /// </summary>
        /// <param name="procode"></param>
        /// <param name="Pro_type_code"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<Db_Product> Get_Products_Other(string procode, string Measure_id, int currentPage, int pageSize)
        {
            IQueryable<Db_Product> query = myDbContext.Db_Product.Where(p => p.Del_flag == '0');

            


            int Measure_id_int=0;
            if (!string.IsNullOrEmpty(Measure_id))
            {
                Measure_id_int = int.Parse(Measure_id);
            }


            if (!string.IsNullOrEmpty(procode))
            {
              query=query.Where(P=>P.Pro_code==procode);
            }
            if (Measure_id_int!=0)
            {
                query = query.Where(S => S.Measure_id == Measure_id_int);
            }
            return query.Skip((currentPage - 1) * pageSize).Take(pageSize).OrderBy(p => p.Pro_sort).ToList();
        }






        /// <summary>
        /// 查询产品信息总数--其他入库单
        /// </summary>
        /// <param name="procode"></param>
        /// <param name="Pro_type_code"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int ProductCount_Other(string procode, string Measure_id)
        {
            IQueryable<Db_Product> query = myDbContext.Db_Product.Where(p => p.Del_flag == '0');


            int Measure_id_int=0;
            if (!string.IsNullOrEmpty(Measure_id))
            {
                Measure_id_int = int.Parse(Measure_id);
            }

            if (!string.IsNullOrEmpty(procode))
            {
                query = query.Where(P => P.Pro_code == procode);
            }
            if (Measure_id_int != 0)
            {
                query = query.Where(S => S.Measure_id == Measure_id_int);
            }
            return query.Count();
        }

        /// <summary>
        /// 添加产品基础信息
        /// </summary>
        /// <param name="db_Product"></param>
        /// <returns></returns>
        public async Task<string> ProductAddAsync(Db_Product db_Product)
        {
            await myDbContext.Db_Product.AddAsync(db_Product);
            await myDbContext.SaveChangesAsync();
            return db_Product.Pro_code;
        }

        /// <summary>
        /// 条件查询产品总数
        /// </summary>
        /// <param name="proName"></param>
        /// <param name="typeCode"></param>
        /// <returns></returns>
        public int ProductCount(string proName, string typeCode)
        {
            IQueryable<Db_Product> query = myDbContext.Db_Product.Where(p => p.Del_flag == '0');
            if (!string.IsNullOrEmpty(proName))
            {
                query = query.Where(p => p.Pro_name.Contains(proName) || p.Pro_code.Contains(proName));
            }
            if (!string.IsNullOrEmpty(typeCode))
            {
                query = query.Where(p => p.Pro_type_code == typeCode);
            }
            return query.Count();
        }

    

        /// <summary>
        /// 逻辑删除产品信息
        /// </summary>
        /// <param name="db_Product"></param>
        /// <returns></returns>
        public async Task<int> ProductDelAsync(Db_Product db_Product)
        {
            Db_Product db_Product1 = myDbContext.Db_Product.FirstOrDefault(p => p.Pro_id == db_Product.Pro_id);
            db_Product1.Del_flag = db_Product.Del_flag;
            myDbContext.Update(db_Product1);
            return await myDbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 修改产品信息
        /// </summary>
        /// <param name="db_Product"></param>
        /// <returns></returns>
        public async Task<int> ProductUpdateAsync(Db_Product db_Product)
        {
            Db_Product db_Product1 = await myDbContext.Db_Product.FirstOrDefaultAsync(p => p.Pro_id == db_Product.Pro_id);
            db_Product1.Pro_name = db_Product.Pro_name;
            db_Product1.Pro_code = db_Product.Pro_code;
            db_Product1.Pro_type_code = db_Product.Pro_type_code;
            db_Product1.Measure_id = db_Product.Measure_id;
            db_Product1.Pro_attribute_id = db_Product.Pro_attribute_id;
            db_Product1.Pro_model = db_Product.Pro_model;
            db_Product1.Pro_sort = db_Product.Pro_sort;
            db_Product1.Pro_brand = db_Product.Pro_brand;
            db_Product1.Pro_sale_price = db_Product.Pro_sale_price;
            db_Product1.Pro_cost_price = db_Product.Pro_cost_price;
            db_Product1.Pro_grossMargin = db_Product.Pro_grossMargin;
            db_Product1.Pro_shelf_life = db_Product.Pro_shelf_life;
            db_Product1.Pro_status = db_Product.Pro_status;
            db_Product1.Update_by = db_Product.Update_by;
            db_Product1.Update_time = db_Product.Update_time;
            myDbContext.Db_Product.Update(db_Product1);
            return await myDbContext.SaveChangesAsync();
        }
    }
}
